<?php
namespace Home\Controller;
use Think\Controller;
class UserInfoController extends CommonController {

    public function uptoOrgnizer()
    {
        if(IS_POST){
            $orgnizers =  D('orgnizers');

            if ($orgnizers->where(["uid" => parent::$uid])->select()){
                $rs['code'] = -1;
                $rs['msg'] = '您已经是评测组织者，无需提升！';
                $this->ajaxReturn($rs);
            }

            else{
                if (!$data = $orgnizers->create()){
                    // 防止输出中文乱码
                    header("Content-type: text/html; charset=utf-8");
                    $rs['code'] = -1;
                    $rs['msg'] = $orgnizers->getError();
                    $this->ajaxReturn($rs);
                }
                else{
                    $data['uid'] = parent::$uid;
                    if ($oid = $orgnizers->add($data)){
                        $users = M('users');
                        $u['oid'] = $oid;
                        $users->where(['uid' => parent::$uid])->save($u);

                        $rs['code'] = 1;
                        $rs['msg'] = '成功升级为评测组织者！';
                        $this->ajaxReturn($rs);
                    }
                    else{
                        $rs['code'] = -1;
                        $rs['msg'] = '注册失败！查看日志';
                        $this->ajaxReturn($rs);
                    }
                }
            }
        }
        else{
            $this->display();
        }
    }

    public function edit()
    {

        $rs['username'] = parent::$userinfo['username'];
        $rs['email'] = parent::$userinfo['email'];
        $this->assign('rs', $rs);
        $this->display();

    }

    public function editUsername()
    {
        $newusername = I('username');
        $users = M('users');
        if ($users->where(['username' => $newusername])->select()){
            $rs['msg'] = "用户名已经被占用！";
        }
        else{
            $users->where(['uid' => parent::$uid])->save(['username' => $newusername]);
            $rs['msg'] = "保存成功！";
        }
        $this->ajaxReturn($rs);
    }

    public function editUseremail()
    {
        $newuseremail = I('useremail');
        $users = M('users');
        if ($users->where(['email' => $newuseremail])->select()){
            $rs['msg'] = "邮箱已经被占用！";
        }
        else{
            $users->where(['uid' => parent::$uid])->save(['email' => $newuseremail]);
            $rs['msg'] = "保存成功！";
        }
        $this->ajaxReturn($rs);
    }

    public function editPassword()
    {
        $newpwd = I('newpwd');
        $users = M('users');


        $newpwd = md5($newpwd);
        $users->where(['uid' => parent::$uid])->save(['password' => $newpwd]);
        $rs['msg'] = "保存成功！";

        $this->ajaxReturn($rs);
    }


}